import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/parts/api/v1';

const getInitList = formatMessage({
    id: 'api.getInitList',
    defaultMessage: '查询初始数据'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询订单数据'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询订单数据详情'
});

const getBackOrderDetailDesc = formatMessage({
    id: 'api.getBackOrderDetailDesc',
    defaultMessage: '查询backOrder数据信息'
});

const getDeliverDesc = formatMessage({
    id: 'api.getReceive',
    defaultMessage: '交付订单信息'
});

const closeDesc = formatMessage({
    id: 'api.message.closeDesc',
    defaultMessage: '关闭'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/partShipmentDeliver/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitList));

// 查询订单数据列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/partShipmentDelivers?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询订单数据详情
export const getDetail = id => request(`${baseUrl}/partShipmentDelivers/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

//关闭
export const closeInfo = (id, data) => request(`${baseUrl}/partShipmentDelivers/${id}/close`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, closeDesc));

// 交付
export const deliver = (id, data) => request(`${baseUrl}/partShipmentDelivers/${id}/deliver`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getDeliverDesc));

//获取backOrder数据详情
export const getBackOrderDetail = id => request(`${baseUrl}/partShipmentDelivers/${id}/backorder`, null, dto.BackOrderDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getBackOrderDetailDesc));
// 查询订单数据详情
export const getDetailDeliver = id => request(`${baseUrl}/partShipmentDelivers/${id}/detail`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
